package com.changeover.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.changeover.dao.IUserDao;
import com.changeover.domain.User;

@Service
public class ControlUserService implements IControlUserService {
    @Autowired
    private IUserDao userDao;
    
    @Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	public List<User> getAllUsers() {
    	List<User> users = userDao.getAll();
		return users;
	}
    
    @Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public void saveUser(Integer id, String name,  String passwd) {
    	User user;
    	if (id != null) {
    		user = userDao.get(id);
    		if (user == null) {
    			throw new IllegalStateException("No such user[id=" + id + "]");
    		}
    	} else {
    		user = new User();
    	}
    	user.setName(name);
    	user.setPasswd(passwd);
    	userDao.saveOrUpdate(user);
	}
    
    @Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public void deleteUser(Integer id) {
    	if (id != null) {
    		userDao.delete(id);
    	}
	}
}
